(ns net.marigolda.scraping-start
  (:require [net.cgrand.enlive-html :as html])
  (:require [clojure.string :as str]))

(defn fetch-url [url]
  (html/html-resource (java.net.URL. url)))

(defn blogspot-headlines-a-tags [blogID]
  "Scraper for blogger headlines. Takes id of the blog@blogspot and returns seq of <a> tag headlines"
  (html/select (fetch-url (str "http://" blogID ".blogspot.com")) [:h3.post-title.entry-title :a]))

;; FonForumMaster 5 latest updated posts (list <a> tags)
(def *ff-url* "http://www.fonforum.org/viewforum.php?f=15")

(defn drop-point [row]
  (= "Teme" (first (html/select row [:td.row3 :b.gensmall html/text]))))

(defn ff-master-top5-headlines []
  (map #(update-in % [:attrs :href] (fn [val] (str/replace-first val "./" "http://www.fonforum.org/")))
    (take 5
      (html/select 
        (concat (rest (drop-while #(not (drop-point %)) 
                        (take 15 (html/select (fetch-url *ff-url*) [:div#pagecontent :table.tablebg :tr])))))
        [:td.row1 :a.topictitle]))))

;; master.fon.rs latest 5 headlines
(defn fon-master-top5-headlines []
  (take 5 (html/select (fetch-url "http://master.fon.rs/") [:span.vestinove])))
  
;; groups.google.com scraper
(defn googlegroup-scraper [groupId]
  "Scraper for google group headlines. Takes id of group and returns seq of <a> tags - latest updated topics"
  (map #(update-in % [:attrs :href] (fn [val] (str "http://groups.google.com" val)))
              (html/select (fetch-url (str "http://groups.google.com/group/" groupId)) [:div.moduleFULL :a])))